package com.songcha.module_category.ui.fragment.category

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.ViewModel
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.library_base.base.BaseFragment
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.handler.HomeBookRecommendDialogHandler
import com.songcha.library_business.helper.AdvertCoverHelper
import com.songcha.library_business.helper.AdvertHelper
import com.songcha.library_business.helper.UMengHelper
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.view.BottomNavigationView
import com.songcha.library_common.ui.view.CustomContainerLayout
import com.songcha.library_common.ui.view.CustomTabLayout
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.module_category.R
import com.songcha.module_category.bean.CategoryChannelListBean
import com.songcha.module_category.databinding.CategoryFragmentCategoryBinding
import com.songcha.module_category.ui.activity.MainActivity
import com.songcha.module_category.ui.adapter.CategoryTabAdapter

@Route(path="/category/CategoryFragment")
class CategoryFragment: BaseMvvmFragment<CategoryViewModel,CategoryFragmentCategoryBinding>(),
    BottomNavigationView.IOnBottomNavigationItemChangeListener{

    private var mTab:CustomTabLayout?=null
    private var mVp:ViewPager?=null
    private var mLlSearch:LinearLayout? = null
    private var mIsCurrentPage = false

    init {
        TAG=CategoryFragment::class.java.simpleName
        if(MajiaBaoUtil.getMajiaName() == "douyue"){
            mIsCurrentPage = false
        }else{
            mIsCurrentPage = true
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.category_fragment_category
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        if(mRootView==null) return
        mTab=mRootView!!.findViewById(R.id.category_ctl)
        mVp=mRootView!!.findViewById<ViewPager>(R.id.category_vp)
        mLlSearch = mRootView!!.findViewById<LinearLayout>(R.id.cate_ll_tab)

        val cclSearch = mRootView!!.findViewById<CustomContainerLayout>(R.id.cate_ccl_search)

        if(MajiaBaoUtil.getMajiaName() == "douyue"){
            ViewHelper.setVisibility(false,cclSearch)
        }else{
            initAdvert()
        }

        cclSearch.setOnClickListener {
            BusinessJumpUtil.toARouterActivity("/home/SearchActivity")
        }

        viewModel.categoryList.observe(this){data->
            initVp(data)
        }

    }

    private fun initAdvert(){
        AdvertCoverHelper.loadAdvertByCover(requireActivity(),AdvertHelper.ADVERT_TYPE_INSERT_SCREEN,
            VarConfig.ADVERT_HOME_INSERT_SCREEN_INFO,null,0f,
            object : AdvertHelper.ILoadAdvertCommonListener{
            override fun onSuccess(type: Int, height: Int,view: View?) {

            }

            override fun onError(type: Int,code:Int,msg:String) {

            }

            override fun onClose(type: Int) {

            }

        })
    }

    private fun initVp(list:MutableList<CategoryChannelListBean.ChannelBean>){
        if(list.size==0) return
        mVp!!.adapter=CategoryTabAdapter(childFragmentManager,list)
        mTab!!.setViewPager(mVp!!)
    }

    override fun onGetData() {
        viewModel.getCategoryChannelList()
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun onRootViewPost() {
        if(mLlSearch==null) return
        /*mTab!!.layoutParams=mTab!!.layoutParams.apply {
            this as LinearLayout.LayoutParams
            topMargin=StatusBarUtil.getStatusBarHeight(requireActivity())
        }*/

        mLlSearch!!.layoutParams=mLlSearch!!.layoutParams.apply {
            this as LinearLayout.LayoutParams
            topMargin=StatusBarUtil.getStatusBarHeight(requireActivity())+10.dp2px()
        }
    }


    override fun onResume() {
        super.onResume()
        if(MajiaBaoUtil.getMajiaName() == "douyue" && mIsCurrentPage)
            HomeBookRecommendDialogHandler.handleShowHomeBookRecommendDialog(requireContext()) { it ->
                viewModel.getHomeRecommendDialogBook { it2 ->
                    it.invoke(it2)
                }
            }
    }

    override fun onStop() {
        super.onStop()
        if(MajiaBaoUtil.getMajiaName() == "douyue")
            HomeBookRecommendDialogHandler.handleInterruptHomeBookRecommendDialog()
    }

    override fun onBottomNavigationItemChange(position: Int,tag:String) {
        if(tag==TAG){
            mIsCurrentPage = true
            //onGetData()
            UMengHelper.onUMEvent(requireContext(),"bottom_tab_category")

            if(MajiaBaoUtil.getMajiaName() == "douyue")
                HomeBookRecommendDialogHandler.handleShowHomeBookRecommendDialog(requireContext()) { it ->
                    viewModel.getHomeRecommendDialogBook { it2 ->
                        it.invoke(it2)
                    }
                }
        }else{
            mIsCurrentPage = false
            if(MajiaBaoUtil.getMajiaName() == "douyue")
                HomeBookRecommendDialogHandler.handleInterruptHomeBookRecommendDialog()
        }
    }

}